var app = getApp();
var WxParse = require('../../wxParse/wxParse.js');
var Data = require('../../utils/data.js');
var common = require("../../utils/common.js")  
var that,mobile;

Page({

	/**
	 * 页面的初始数据
	 */
	data : {
		// 判断小程序的API，回调，参数，组件等是否在当前版本可用。
		canIUse : wx.canIUse('button.open-type.getUserInfo'),
    invite_code:''
	},

	/**
	 * 生命周期函数--监听页面加载
	 */
	onLoad : function(options) {
		that = this;
    if (options.invite_code && options.invite_code != "undefined"){
      console.log("reg:" + options.invite_code);
      that.setData({ invite_code: options.invite_code});
      app.globalData.invite_code = options.invite_code;
      common.setStorage("yq_invite_code", options.invite_code);
    }
		// 查看是否授权
		/*wx.getSetting({
			success: function(res){
				if (res.authSetting['scope.userInfo']) {
					wx.getUserInfo({
						success: function(res) {
							console.log(res.userInfo)
							// 用户已经授权过
						}
					})
				}
			}
	    })*/
	},

	/**
	 * 生命周期函数--监听页面初次渲染完成
	 */
	onReady : function() {

	},

	/**
	 * 生命周期函数--监听页面显示
	 */
	onShow : function() {

	},

	/**
	 * 生命周期函数--监听页面隐藏
	 */
	onHide : function() {

	},
  onShareAppMessage: function (res) {
    return {
      title: '对不起~打扰了，我就想请你来亿享~',
      path: '/pages/index/index?invite_code=' + common.getStorage("invite_code")
    }
  },

	/**
	 * 生命周期函数--监听页面卸载
	 */
	onUnload : function() {

	},

	/**
	 * 页面相关事件处理函数--监听用户下拉动作
	 */
	onPullDownRefresh : function() {

	},

	/**
	 * 页面上拉触底事件的处理函数
	 */
	onReachBottom : function() {

	},

	bindinputmobile:function(e){
		mobile = e.detail.value;
	},
	
	bindGetUserInfo: function(e) {
    var inviteCode = e.currentTarget.dataset.invite ? e.currentTarget.dataset.invite:"";
    console.log("invite_code:" + inviteCode);
		if(common.isBlank(mobile)){
			common.showInfo('请输入手机号'); 
			return false;
		}
		if(common.isPoneAvailable(mobile)==false){
			common.showInfo('请输入正确手机号');
			return false;
		}
    
		common.wxshowLoading();
    if (e.detail.userInfo) {
      //用户按了允许授权按钮
      wx.login({
        success: function (res) {
          if (res.code) {
            wx.getUserInfo({
              withCredentials: false,
              success: function (res2) {
                wx.setStorage({
                  key: "nickname",
                  data: res2.userInfo.nickName
                });
                wx.setStorage({
                  key: "avatarUrl",
                  data: res2.userInfo.avatarUrl
                });

                //登录
                Data.httpGet({
                  action: 'login',
                  code: res.code,
                  headimgurl: res2.userInfo.avatarUrl,
                  nickname: res2.userInfo.nickName,
                  sex: res2.userInfo.gender,
                  invite_code: inviteCode,
                  location: res2.userInfo.country + ' ' + res2.userInfo.province + ' ' + res2.userInfo.city,
                  mobile: mobile
                }, function (res) {
                  common.wxhideLoading();
                  if (res.data.openid) {
                    wx.setStorage({
                      key: "openid",
                      data: res.data.openid
                    });
                    wx.setStorage({
                      key: "user_id",
                      data: res.data.user_id
                    });
                    wx.setStorage({
                      key: "invite_code",
                      data: res.data.invite_code
                    });
                    wx.setStorage({
                      key: "session_key",
                      data: res.data.session_key
                    });
                    wx.setStorage({
                      key: "mobile",
                      data: res.data.mobile
                    });
                    wx.switchTab({
                      url: '../index/index'
                    })

                  } else {
                    //console.log("登录失败");
                    //console.log(res.data);
                  }
                });




              }, fail(res) {
                console.log(res);
              }
            })

          } else {
            //console.log('获取用户登录态失败！' + res.errMsg)
          }
        }
      });
	    	
	    	
	    	
	    } else {
	    	//用户按了拒绝按钮
	    	wx.showModal({}) 
	    }
	},


})